package com.ljb.commlibrary.okgo;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.callback.AbsCallback;

import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Created by ljb on 2018/5/2.
 */

public abstract class JsonCallback<T> extends AbsCallback<T> {
    private Class<T> clazz;

    public JsonCallback(Class<T> clazz) {
        this.clazz = clazz;
    }

    @Override
    public T convertResponse(Response response) throws Throwable {
        ResponseBody body = response.body();
        if (body == null || clazz == null)
            return null;
        return JSON.parseObject(body.bytes(), clazz);
    }
}
